<?php
/**
 * Classe Complement de produit
 *
 * Description de la classe
 *
 * @author Mathieu
 */
class ProductComplement extends Object {
    protected $id;
    protected $produit_id;
    protected $stock;
    protected $nom;

    function __construct($id="", $params=array()) {
        if($id!='') {
            $this->id = $id;
            foreach($params as $key => $p) {
                $this->$key = $p;
            }
        }
    }
    function __toString() {
        if(!isset($this->nom)) $this->load();
        return $this->nom;
    }

    public function load() {
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM complement WHERE id = '".$this->id."'");
        if(sizeof($result) > 0) {
            foreach($result[0] as $key => $p) {
                $this->$key = $p;
            }
            return;
        }
        if(DEBUG) echo("Objet inconnu");
        return false;
    }

    public function save() {
        $db = Database::getInstance();
        if (!isset($this->id)) {
            $sql = "INSERT INTO complement (produit_id,stock,nom)
                        VALUES  ('".$this->produit_id."',
                                '".$this->stock."',
                                '".$this->nom."'
                                )";
            $result = $db->query($sql);
            $this->id=$db->getInsertedId();
        }
        else{
            $sql = "UPDATE complement SET  brand_id='".$this->produit_id."',
                                    famille_id='".$this->stock."',
                                    bigtag_id='".$this->nom."'
                                WHERE  id='".$this->id."'  ";
            $result = $db->query($sql);
        }
    }
	public function getSize() {
		 if(!isset($this->nom)) $this->load();
		 $taille=strrchr($this->nom,' ');
		 return $taille;
	
	
	}
	
	public function getColor() {
		
		if(!isset($this->nom)) $this->load();
		$couleur=str_replace($this->getSize(),'',$this->nom);
		return $couleur;
	
	}

    public function delete(){
        $db= Database::getInstance();
        $sql="DELETE FROM complement WHERE id = '".$this->id."' ";
        $db->query($sql);
    }

}